package org.lnstark.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * by Lnstark
 * 2021/6/19
 */
public class JsonUtil {
    public static <T> T toObject(String json, Class<T> clz) {
        T o = null;
        ObjectMapper om = new ObjectMapper();
        try {
            o = om.readValue(json, clz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static String toJsonString(Object o) {
        String json = null;
        ObjectMapper om = new ObjectMapper();
        try {
            json = om.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
}
